<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Moodle guest book block..
*
* @package block_moodlebook
* @copyright Konovalov Nikolay <konovalov.nk@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

global $CFG, $DB, $OUTPUT, $PAGE;

require_once($CFG->dirroot . '/config.php');
require_once($CFG->dirroot . '/blocks/moodlebook/lib/moodlebook_forms.php');

// Check for all required variables.
$courseid = required_param('courseid', PARAM_INT);
$blockid = required_param('blockid', PARAM_INT);
$messageid = required_param('messageid', PARAM_INT);
$commentid = required_param('commentid', PARAM_INT);
 
// Next look for optional variables.
$id = optional_param('id', 0, PARAM_INT);


if (!$course = $DB->get_record('course', array('id' => $courseid))) {
    print_error('invalidcourse', 'block_moodlebook', $courseid);
}

require_login($course);

$PAGE->set_url('/blocks/moodlebook/view.php', array('id' => $courseid));
$PAGE->set_pagelayout('standard');
$PAGE->set_heading(get_string('edithtml', 'block_moodlebook'));

$settingsnode = $PAGE->settingsnav->add(get_string('moodlebooksettings', 'block_moodlebook'));
$editurl = new moodle_url('/blocks/moodlebook/view.php', array('id' => $id, 'courseid' => $courseid, 'blockid' => $blockid));
$editnode = $settingsnode->add(get_string('editpage', 'block_moodlebook'), $editurl);
$editnode->make_active();

$submitform = new moodlebook_submitform(null, array('messageid' => $messageid, 'commentid' => $commentid ));

if($submitform->is_cancelled()) {
    // Cancelled forms redirect to the course main page.
    $courseurl = new moodle_url('/course/view.php', array('id' => $id));
    redirect($courseurl);
} else if ($submitform->get_data()) {
    // We need to add code to appropriately act on and store the submitted data
    // but for now we will just redirect back to the course main page.
    $courseurl = new moodle_url('/course/view.php', array('id' => $courseid));
    redirect($courseurl);
} else {
    // form didn't validate or this is the first display
    $site = get_site();
    echo $OUTPUT->header();
    $submitform->display();
    echo $OUTPUT->footer();
}

?>